/**	
 * <br>
 * Copyright 2011 IFlyTek. All rights reserved.<br>
 * <br>			 
 * Package: com.iflytek.utils <br>
 * FileName: Code2MCUtil.java <br>
 * <br>
 * @version
 * @author Administrator
 * @created 2012-5-12
 * @last Modified 
 * @history
 */

package com.iflytek.ci.common;

import com.iflytek.iframework.business.cache.impl.OrgCacheLoadImpl;
import com.iflytek.iframework.orm.Page;

import java.lang.reflect.Field;
import java.util.List;

/**
 * {根据系统字典缓存替换编码显示}
 * 
 * @author jbyu
 * @created 2012-5-12 下午05:13:33
 * @lastModified
 * @history
 */
public class Code2MCUtil {

    /**
     * 
     * ｛转换单个实体编码到名称｝
     * 
     * @param <T>
     * @param t
     * @author jbyu
     * @created 2012-5-24 下午12:34:08
     * @lastModified
     * @history
     */
    public static <T> void Code2MC(T t) {
        try {
            Class<?> objclass = t.getClass();
            Field[] at = objclass.getDeclaredFields();
            for (Field fd : at) {
                if (fd.isAnnotationPresent(com.iflytek.ci.base.Code2MC.class)) {
                    com.iflytek.ci.base.Code2MC d = fd
                            .getAnnotation(com.iflytek.ci.base.Code2MC.class);
                    fd.setAccessible(true);
                    Field codeField = objclass.getDeclaredField(d.dmFieldMc());
                    String code = "";
                    if (codeField != null) {
                        codeField.setAccessible(true);
                        Object temp = codeField.get(t);
                        if (temp != null) {
                            code = temp.toString();
                        }
                    }
                    if (d.isOrg()) {
                        String org = OrgCacheLoadImpl.getOrgMc(code, true);
                        if (org != null) {
                            fd.set(t, org);
                        } else {
                            fd.set(t, code);
                        }
                    } else {
                        String mc = TransferUtil.getZdmc(d.lxjp(), code);
                        if (mc != null) {
                            fd.set(t, mc);
                        } else {
                            fd.set(t, code);
                        }

                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 
     * ｛转换集合内的所有实体到编码到名称｝当集合数量小于30时使用此方法
     * 
     * @param <T>
     * @param className
     * @param list
     * @author jbyu
     * @created 2012-5-24 下午12:34:35
     * @lastModified
     * @history
     */
    public static <T> void Code2MC(Class<?> className, List<T> list) {
        try {
            Class<?> objclass = className;
            Field[] at = objclass.getDeclaredFields();
            for (Field fd : at) {
                if (fd.isAnnotationPresent(com.iflytek.ci.base.Code2MC.class)) {
                    com.iflytek.ci.base.Code2MC d = fd
                            .getAnnotation(com.iflytek.ci.base.Code2MC.class);
                    fd.setAccessible(true);
                    Field codeField = objclass.getDeclaredField(d.dmFieldMc());
                    for (T t : list) {
                        String code = "";
                        if (codeField != null) {
                            codeField.setAccessible(true);
                            Object temp = codeField.get(t);
                            if (temp != null) {
                                code = temp.toString();
                            }
                        }
                        if (d.isOrg()) {
                            String org = OrgCacheLoadImpl.getOrgMc(code, true);
                            if (org != null) {
                                fd.set(t, org);
                            } else {
                                fd.set(t, code);
                            }
                        } else {
                            String mc = TransferUtil.getZdmc(d.lxjp(), code);
                            if (mc != null) {
                                fd.set(t, mc);
                            } else {
                                fd.set(t, code);
                            }
                        }
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 
     * ｛说明该函数的含义和作用，如果函数较为复杂，请详细说明｝
     * 
     * @param <T>
     * @param className
     * @param page
     * @author sbwang@iflytek.com
     * @created 2013-6-3 下午10:54:15
     * @lastModified
     * @history
     */
    public static <T> void Code2MC(Class<?> className, Page<T> page) {
        Code2MC(className, page.getResult());
    }

    /**
     * 
     * ｛大数据量需要转换编码到名称｝
     * 
     * @param <T>
     * @param className
     * @param list
     * @author jbyu
     * @created 2012-5-24 下午12:36:56
     * @lastModified
     * @history
     */
    public static <T> void Code2MCLongList(Class<?> className, List<T> list) {
        for (T t : list) {
            Code2MC(t);
        }
    }

}
